package net.lovesrc.allin.controller;

import com.zhouzhou.fastjava.util.FileKit;
import net.lovesrc.allin.Message;
import net.lovesrc.allin.biz.FileService;
import net.lovesrc.allin.biz.NotificationService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@RequestMapping("/tools")
public class ToolsController extends BaseController {

    @Autowired
    private NotificationService notificationService;

    @Autowired
    private FileService fileService;

    @PostMapping("/email")
    public Message String(String text) {
        notificationService.syncEmail(NotificationService.LZQ_EMAIL, text);
        return Message.success();
    }

    @GetMapping("/get")
    public void get(String url, HttpServletResponse response) throws IOException {
        File file = fileService.save(url);
        response.setContentType(FileKit.getConentType(file.getName()));
        IOUtils.copy(new FileInputStream(file), response.getOutputStream());
    }

}
